// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Layouts
import Qt5Compat.GraphicalEffects
import QtQuick.Controls
import Fk
import Fk.Pages
import Fk.RoomElement


/* Layout of card:
 *      +--------+
 *      |        |
 *      |  img   |
 *      |        |
 *      |        |
 *      |price   |
 *      +--------+
 */

Item {
  id: root

  // properties for the view
  property string name: "chosen"
  property string picsource: "chosen"
  property string extension: ""
  property string virt_name: ""
  property int type: 0
  property int pwidth: 135
  property int pheight: 180

  property string subtype: ""
  property string color: ""  // only use when suit is empty
  property string footnote: ""  // footnote, e.g. "A use card to B"
  property bool footnoteVisible: false
  property string prohibitReason: ""
  property bool known: true     // if false it only show a card back
  property bool enabled: true   // if false the card will be grey
  property alias card: cardItem
  property alias glow: glowItem
  property var mark: ({})
  //property alias chosenInBox: chosen.visible
  width: pwidth
  height: pheight


  // properties for animation and game system
  property int cid: 0
  property bool selectable: true
  property bool selected: false
  property int total: 5
  property int selectdnum: 0
  property bool draggable: false
  property bool autoBack: true
  property bool showDetail: true

  signal toggleDiscards()
  signal clicked()
  signal rightClicked()
  signal doubleClicked()
  signal thrown()
  signal released()
  signal entered()
  signal exited()
  signal moveFinished()
  signal generalChanged()   // For choose general freely
  signal hoverChanged(bool enter)




  RectangularGlow {
    id: glowItem
    anchors.fill: parent
    glowRadius: 8
    spread: 0
    color: "#88FFFFFF"
    cornerRadius: 8
    visible: true
  }

  Image {
    id: cardItem
    source: ''+picsource
    anchors.fill: parent
    fillMode: Image.PreserveAspectCrop
  }


  GlowText {
    id : totaltext
    text: total.toString();
    visible :  total > 0
    //font.family: fontLibian.name
    font.pixelSize: 15
    font.bold: true
    color: "#FE2222"
    glow.color: "#FEF7D6"
    glow.spread: 0.5
    anchors.right: parent.right
    anchors.bottom: parent.bottom
  }

  GlowText {
    id : selectdnumtext
    text: selectdnum.toString();
    visible : selectdnum > 0
    //font.family: fontLibian.name
    font.pixelSize: 30
    font.bold: true
    color: "#FE2222"
    glow.color: "#FEF7D6"
    glow.spread: 0.5
    anchors.centerIn: parent
  }

  TapHandler {
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (btn === Qt.LeftButton || btn === Qt.NoButton) {
        selected = selectable ? !selected : false;
        parent.clicked();
      } else if (btn === Qt.RightButton) {
        parent.rightClicked();
      }
    }

    onLongPressed: {
      parent.rightClicked();
    }
  }

  DragHandler {
    id: drag
    enabled: draggable
    grabPermissions: PointHandler.TakeOverForbidden
    xAxis.enabled: true
    yAxis.enabled: true

    onGrabChanged: (transtition, point) => {
      if (transtition !== PointerDevice.UngrabExclusive) return;
      parent.released();
      if (autoBack)
        goBackAnimation.start();
    }
  }

  HoverHandler {
    id: hover
    onHoveredChanged: {
      if (!draggable) return;
      if (hovered) {
        glow.visible = true;

        root.z = root.maxZ ? root.maxZ + 1 : root.z + 1;
      } else {
        glow.visible = false;

        root.z = root.initialZ ? root.initialZ : root.z - 1
      }
    }
  }



}
